#ifndef USER_GlOBAL_H
#define USER_GlOBAL_H
// user_global defines ()
#include "gd32f4xx.h"
#include <math.h>
#include "user_config.h"

// user_def
#define XDATA __attribute__((section("XRAM")))
#define xdata __attribute__((section("XRAM")))
#define IDATA __attribute__((section("DTCM")))
#define XDATA __attribute__((section("XRAM")))

#ifndef bit
#define bit unsigned char
#endif
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef ushort
#define ushort unsigned short
#endif
#ifndef ulong
#define ulong unsigned long
#endif
#ifndef uint
#define uint unsigned long
#endif

#ifndef u8
#define u8 unsigned char
#define U8 unsigned char
#endif
#ifndef u16
#define u16 unsigned short
#define U16 unsigned short
#endif
#ifndef u32
#define u32 unsigned long
#define U32 unsigned long
#endif

// #define UNUSED(x)        ((x)=(x))
#ifndef _STR
#define __STR(x) #x
#define _STR(x) __STR(x)
#endif

#ifndef assert
#define assert(expr)
#endif

typedef union tagUU16
{
    U8 UU8[2];
    U16 UU16;
} UU16;

#define USE_BY_USART1
//------------------------------------------------------------------------------
// 常量定义
//------------------------------------------------------------------------------

// 系统频率
#define SYSCLK INTCLK

// #define UNUSED(x)    ((x)=(x))

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

#define TIMER_STOPED 0xFFFFFFFF
//------------------------------------------------------------------------------
// 数据类型
//------------------------------------------------------------------------------

typedef ushort Route;

// 将多字节数转换为小端模式
extern ushort from_little_endian_16(ushort n);
// 将多字节数转换为小端模式
extern ulong from_little_endian_32(ulong n);

#define MSB 1
#define LSB 0

typedef struct FILE_DESC
{
    ushort mask;
    ushort count;
    union
    {
        ushort *w;
        float *f;
    } ptr;
} FILE_DESC;

enum MACHINE_ROLE
{
    S1 = 0, // 左推入电机
    S2,     // 右推入电机
    V1,     // 左输入闸门电机
    V2,     // 右输入闸门电机
    V3,     // 输出闸门电机
    T1,     // 主轨道传送带电机
    R1,     // 转管
    R2,     // 升降
    MACHINE_COUNT
};

enum ACTION_PENDING_ROLE
{
    ACTION_PENDING_MODBUS_EVENT = 0,
    ACTION_PENDING_KEY1_EVENT,
    ACTION_PENDING_KEY2_EVENT,
};

__packed typedef struct MACINE_INFO
{
    ushort duration;
    long distance;
} MACHINE_INFO;

__packed typedef struct MACINE_PARAM
{
    ushort max_speed;
    long max_distance;
} MACHINE_PARAM;

__packed typedef struct APP_PARAM
{
    uchar disable_scanner : 1;
    uchar disable_execonditon : 1;
    uchar ignore_importl_ready : 1;
    uchar ignore_importr_ready : 1;
    uchar ignore_export_ready : 1;
    ushort reserverd : 11;
} APP_PARAM;

// 轨道单元内的站点
enum STATION
{
    IMPORTL = 0,
    IMPORTR,
    EXPORT,

    STATION_COUNT,
    STATION_NULL = STATION_COUNT
};

////////////////////////////////////////////////////////////////////////////////
//

typedef struct READY_SEN
{
    bit state : 1;
    bit active : 1;
} REAYD_SEN;

typedef struct FIX_VALUE
{
    uchar index;
    uchar enable; //
    uchar state;  //
    uchar exist;  // D4:0 表示对应样本n存在； D5 表示存在样本位置失效
    uchar hat_exist;
    ushort step;
} FIX_VALUE;

typedef struct FIX_FILE
{
    ushort read;
    ushort write;
    struct FIX_VALUE value[1024];
} FIX_FILE;

extern FIX_FILE fix_file;

enum
{
    KEY_STARTL = 0,
    KEY_STARTR,
    KEY_STOP,

    KEY_START_COUNT = KEY_STARTR + 1,

    KEY_COUNT = KEY_STOP + 1,
};

enum KEY_STATE
{
    KEY_STATE_IDLE = 0, // 空闲状态
    KEY_STATE_WAIT,     // 处于等待状态
    KEY_STATE_WORKING,  // 处于工作状态
    KEY_STATE_RETRY,
    KEY_STATE_DONE,
};

enum MACHINE_STATE
{
    ME_STATE_IDLE = 0, // 空闲状态
    ME_STATE_WAIT,     // 处于等待状态
    ME_STATE_WORKING,  // 处于工作状态，工作状态下检测到可运行，启动动作
    ME_STATE_RETRY,
    ME_STATE_RESET, // 需要直接复位
    ME_STATE_DONE,
};

enum Event
{
    EVENT_NONE = 0,
    EVENT_BUF_EMPTY,          // 缓冲区为空
    EVENT_LIMIT_ERROR,        // 推送机构限位传感器错误
    EVENT_EXPORT_READY_ERROR, // 输出到位传感器错误
    EVENT_FINISHED
};

// 条码运行状态
enum BARSTATE
{
    SCANBAR_IDLE = 0,
    SCANBAR_RUNNING,
};

// 条码类型
enum CODETYPE
{
    POSITION_CODE = 0, // 位置码
    SAMPLE_CODE,       // 样本码
};

typedef struct SCANBAR
{
    uchar read;                             // 条码读缓存索引
    uchar write;                            // 条码写缓存索引
    uchar last_pos;                         // 前一次读取的位置码的位置
    uchar recent_pos;                       // 最后读取的位置码的位置
    uchar sample_pos;                       // 读取到样本码时最后一次读到的位置码位置
    uchar sample_index;                     // 当前样本架号(a ,b , c, d)
    uchar index;                            // 当前读取的条码数目索引
    uchar state;                            // 当前状态
    uchar barcount;                         // 读取到的条码数量
    char barcode[MAX_BAR_NUMBER][20];       // 读取到的条码缓存
    char barlen[MAX_BAR_NUMBER];            // 读取到的条码缓存对应的长度
    uchar type[MAX_BAR_NUMBER];             // 条码类型索引，0位置码/1样本码
    uchar lasttype;                         // 前一次扫码类型,0位置码/1样本码,255 NULL
    char spare_barcode[MAX_BAR_NUMBER][20]; // 多余的样本码
    uchar spare_index;                      // 多余的条码写索引
} SCANBAR;
extern SCANBAR mbar;
typedef enum TrackExitMotorSite
{
    // First site of the block motor
    TRACK_EXIT_MOTOR_SITE_START = 0,
    TRACK_EXIT_MOTOR_SITE_FIRST,
    // Second site of the block motor
    TRACK_EXIT_MOTOR_SITE_SECOND,
    // Third site of the block motor
    TRACK_EXIT_MOTOR_SITE_THIRD,
    // Fourth site of the block motor
    TRACK_EXIT_MOTOR_SITE_FOURTH,
    // Fifth site of the block motor
    TRACK_EXIT_MOTOR_SITE_FIFTH,
    // Origin site of the block motor
    TRACK_EXIT_MOTOR_SITE_ORIGIN,

    TRACK_EXIT_MOTOR_SITE_COUNT,
}TrackExitMotorSite;

struct TransportIn
{
    TrackExitMotorSite site;          // Current site of the block motor
    TrackExitMotorSite site_previous; // Previous site of the block motor
    int testTubeSite; // 1 if testTubeSite is active
    uint32_t stamp; // Time stamp
    uint32_t endflag;
    uint32_t sampleRackOnConveyor;
    uint32_t nextTubeExist;
    uint32_t isTallTube;
    uint32_t ScannedBarcode;
};
extern struct TransportIn transportIn;
// Enum for the site of the block motorTrackExitMotor

// 指令接收检验
typedef struct SCANBAR_CHECK
{
    uchar len;     // 检验长度
    uchar buf[10]; // 检验内容

} SCANBAR_CHECK;

typedef struct SCAN_CNTRL
{
    ulong t[4];         // 时间基数
    uchar both_open[4]; // 样本架输入输出光耦同时打开时为1

} SCAN_CNTRL;
extern SCAN_CNTRL scnt;

__packed typedef struct LED_CNTRL
{
    ushort led_blink_times;  // 闪烁次数
    ushort led_on_duration;  // 亮时间，ms
    ushort led_off_duration; // 灭时间，ms
} LED_CNTRL;

//  beep 蜂鸣器
enum BEEP_MODE
{
    BEEP_INFO = 0,
    BEEP_INFO2,
    BEEP_WARNING,
    BEEP_ERR,
    BEEP_FATAL,

    BEEP_USER,
    BEEP_USER_KEY_PUSHED = BEEP_USER,
    BEEP_NULL,
};

#endif // CONFIG_H
